﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BusinessLogic;
using BusinessObject;
using Utility;
using System.Web.Security;
using System.Resources;
namespace WebTeamplate
{
    public partial class Admin_DMPhieuChuyenKho : BasePage
    {
        #region Fields
        public string urlDetail = "PhieuChuyenKho.aspx";
        int count = 0;

        #endregion

        #region ViewState
        public int PageIndex
        {

            get
            {

                object o = ViewState["PageIndex"];

                if (o != null) return (int)o;

                return 0;

                //a default

            }

            set { ViewState["PageIndex"] = value; }

        }
        public string SortField
        {

            get
            {
                object o = ViewState["SortField"];
                if (o != null) return (string)o;
                return string.Empty;
            }

            set { ViewState["SortField"] = value; }

        }
        public string SortExpression
        {

            get
            {
                object o = ViewState["SortExpression"];
                if (o != null) return (string)o;
                return string.Empty;
            }

            set { ViewState["SortExpression"] = value; }

        }
        #endregion

        #region Init,Load
        protected void Page_Load(object sender, EventArgs e)
        {
            ClientDefaultButton.SetPageDefaultButton(this.Page, btnSearch.ClientID);
            if (!Page.IsPostBack)
            {
                InitData();
                LoadData();

            }

        }
        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            InitTextControl();
            this.imgNgayNhapSortDown.ImageUrl = CommonFunction.DEFAULT_SORTDOWN_IMAGEURL;
            this.imgNgayNhapSortUp.ImageUrl = CommonFunction.DEFAULT_SORTUP_IMAGEURL;
            this.imgKhoFromSortDown.ImageUrl = CommonFunction.DEFAULT_SORTDOWN_IMAGEURL;
            this.imgKhoFromSortUp.ImageUrl = CommonFunction.DEFAULT_SORTUP_IMAGEURL;

          

            this.imgKhoToSortDown.ImageUrl = CommonFunction.DEFAULT_SORTDOWN_IMAGEURL;
            this.imgKhoToSortUp.ImageUrl = CommonFunction.DEFAULT_SORTUP_IMAGEURL;

            imgNgayNhapSortDown.Click += new ImageClickEventHandler(imgNgayNhapSortDown_Click);
            imgNgayNhapSortUp.Click += new ImageClickEventHandler(imgKhoFromSortDown_Click);
            imgKhoFromSortDown.Click += new ImageClickEventHandler(imgKhoFromSortUp_Click);
            imgKhoFromSortUp.Click += new ImageClickEventHandler(imgKhoFromSortUp_Click);           
            imgKhoToSortDown.Click += new ImageClickEventHandler(imgKhoToSortDown_Click);
            imgKhoToSortUp.Click += new ImageClickEventHandler(imgKhoToSortUp_Click);
        }

        void imgKhoToSortUp_Click(object sender, ImageClickEventArgs e)
        {
            this.SortField = "kd.SubCategoryName";
            this.SortExpression = CommonFunction.DEFAULT_SORTUP_EXPRESSION;
            LoadData();
        }

        void imgKhoToSortDown_Click(object sender, ImageClickEventArgs e)
        {
            this.SortField = "kd.SubCategoryName";
            this.SortExpression = CommonFunction.DEFAULT_SORTDOWN_EXPRESSION;
            LoadData();
        }      
       
        void imgKhoFromSortUp_Click(object sender, ImageClickEventArgs e)
        {
            this.SortField = "kc.SubCategoryName";
            this.SortExpression = CommonFunction.DEFAULT_SORTUP_EXPRESSION;
            LoadData();
        }

        void imgKhoFromSortDown_Click(object sender, ImageClickEventArgs e)
        {
            this.SortField = "kc.SubCategoryName";
            this.SortExpression = CommonFunction.DEFAULT_SORTDOWN_EXPRESSION;
            LoadData();
        }
        #endregion

        #region Methods

        public void InitTextControl()
        {
            btnSearch.Text = Resources.Resource.LABEL_BUTTON_SEARCH;
            btnReset.Text = Resources.Resource.LABEL_BUTTON_RESET;
            btnAddNew.Text = Resources.Resource.LABEL_BUTTON_ADDNEW;
            btnDelete.Text = Resources.Resource.LABEL_DELETE;
            this.SortExpression = "DESC";
            this.SortField = "NgayNhap";
        }
        private void InitData()
        {
            vsumSumary.ValidationGroup = cusvNgayLapPhieuFrom.ValidationGroup = cusvNgayLapPhieuTo.ValidationGroup = btnSearch.ValidationGroup = "phieuhang";
            cusvNgayLapPhieuFrom.ErrorMessage = cusvNgayLapPhieuTo.ErrorMessage = string.Format(Resources.Resource.MESSAGE_VALIDATE_INPUT, CommonFunction.GetTextLabel(lblNgayNhap));
            cusvNgayLapPhieuFrom.ValidationGroup = cusvNgayLapPhieuTo.ValidationGroup = "phieuhang";
            this.PageIndex = 1;
            Pager1.PageSize = CommonFunction.DEFAULT_ADMIN_PAGE_SIZE;
            CommonFunction.LoadSubCate(drlKhoFrom, CommonFunction.DEFAULT_WAREHOUSE_CODE, true);
            CommonFunction.LoadSubCate(drlKhoTo, CommonFunction.DEFAULT_WAREHOUSE_CODE, true);
          //  CommonFunction.LoadSubCate(drlChungXe, CommonFunction.DEFAULT_CHUNGLOAIXE_CODE, true);
        }

        void imgNgayNhapSortUp_Click(object sender, ImageClickEventArgs e)
        {
            this.SortField = "ph.[ngayNhap]";
            this.SortExpression = CommonFunction.DEFAULT_SORTUP_EXPRESSION;
            LoadData();
        }

        void imgNgayNhapSortDown_Click(object sender, ImageClickEventArgs e)
        {
            this.SortField = "ph.[ngayNhap]";
            this.SortExpression = CommonFunction.DEFAULT_SORTDOWN_EXPRESSION;
            LoadData();
        }

        public void LoadData()
        {
            hidListId.Value = string.Empty;
            hidListIdSelect.Value = string.Empty;
            PhieuChuyenKho objPhieuChuyen = new PhieuChuyenKho();
            objPhieuChuyen.ObjectSortOrder.SortExpression = this.SortExpression;
            objPhieuChuyen.ObjectSortOrder.SortField = this.SortField;
            objPhieuChuyen.ObjectPaging.PageIndex = this.PageIndex;
            objPhieuChuyen.ObjectPaging.PageSize = Pager1.PageSize;
            //objPhieuNhap.Condition = " where categoryId='" + CommonFunction.DEFAULT_WAREHOUSE_CODE + "'";
            objPhieuChuyen.Condition = " where 1=1 ";
            if (drlKhoFrom.SelectedValue != string.Empty)
                objPhieuChuyen.Condition += " and ph.[maKhoChuyen]=" + drlKhoFrom.SelectedValue;
            if (drlKhoTo.SelectedValue != string.Empty)
                objPhieuChuyen.Condition += " and ph.[maKhoDen]=" + drlKhoTo.SelectedValue;
           //if (d rlChungXe.SelectedValue != string.Empty)
           //     objPhieuChuyen.Condition += " and ph.[chungXe]=" + drlChungXe.SelectedValue;
            if (txtMaMatHang.Text != string.Empty)
                objPhieuChuyen.Condition += " and [dbo].[fcMaMatHangInPhieuChuyenKho]('" + txtMaMatHang.Text.Trim() + "',ph.MaPhieuChuyenKho)=1";
            if (txtMaPhuTung.Text != string.Empty)
                objPhieuChuyen.Condition += " and [dbo].[fcMaPhuTungInPhieuChuyenKho]('" + txtMaPhuTung.Text.Trim() + "',ph.MaPhieuChuyenKho)=1";

           
            if (txtNgayLapPhieuFrom.Text != string.Empty)
            {
                DateTime dt = ConvertValueObject.ParseDateTime(txtNgayLapPhieuFrom.Text);
                objPhieuChuyen.Condition += " and DATEDIFF(day,'" + dt.Year + "-" + dt.Month + "-" + dt.Day + "',ph.ngayNhap)>=0";
            }
            if (txtNgayLapPhieuTo.Text != string.Empty)
            {
                DateTime dt = ConvertValueObject.ParseDateTime(txtNgayLapPhieuTo.Text);
                objPhieuChuyen.Condition += " and DATEDIFF(day,'" + dt.Year + "-" + dt.Month + "-" + dt.Day + "',ph.ngayNhap)<=0";
            }
            List<PhieuChuyenKhoResult> lstResult = PhieuChuyenKhoUtil.SearchPhieuChuyenKhoSomeCondition(objPhieuChuyen);
            if (lstResult != null && lstResult.Count > 0)
                count = lstResult.Count;
            rptData.DataSource = lstResult;
            rptData.DataBind();
            Pager1.SetPaging(this.PageIndex, count, objPhieuChuyen.ToltalRow);
        }
        private void Delete(string id)
        {
            if (PhieuChuyenKhoUtil.Delete(ConvertValueObject.ParseLong(id)))
                LoadData();
        }


        #endregion

        #region Events,Handler
       
        protected void rptData_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                PhieuChuyenKhoResult cate = (PhieuChuyenKhoResult)e.Item.DataItem;
                if (cate == null)
                    return;
                Literal ltr = (Literal)e.Item.FindControl("ltrNo");
                ltr.Text = (e.Item.ItemIndex + 1).ToString();

                //ltr = (Literal)e.Item.FindControl("ltrSubCode");
                string url = urlDetail + "?id=" + cate.MaPhieuChuyenKho + "&act=" + CommonFunction.DEFAULT_COMMAND_EDIT;
                //ltr.Text = "<a href='" + url + "'>" + Server.HtmlEncode(cate.SubCategoryCode) + "</a>";
                ltr = (Literal)e.Item.FindControl("ltrNgayLap");
                ltr.Text = ConvertValueObject.ParseString(cate.NgayNhap);

               
                ltr = (Literal)e.Item.FindControl("ltrKhoFrom");
                ltr.Text = Server.HtmlEncode(cate.TenKhoChuyen.ToString());
                ltr = (Literal)e.Item.FindControl("ltrKhoTo");
                ltr.Text = Server.HtmlEncode(cate.TenKhoDen.ToString());
                

                HyperLink hplEdit = (HyperLink)e.Item.FindControl("hplEdit");
                hplEdit.NavigateUrl = url;
                LinkButton lbtnDelete = (LinkButton)e.Item.FindControl("lbtnDelete");
                lbtnDelete.Text = Resources.Resource.LABEL_DELETE;
                lbtnDelete.CommandArgument = cate.MaPhieuChuyenKho.ToString();
                hidListId.Value += cate.MaPhieuChuyenKho + ";";
            }
        }
        protected void rptData_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            string id = e.CommandArgument.ToString();
            switch (e.CommandName)
            {

                case "delete":
                    {
                        Delete(id);
                        break;
                    }

            }

        }


        protected void btnSearch_Click(object sender, EventArgs e)
        {
            this.SortExpression = "DESC";
            this.SortField = "[ngayNhap]";
            //reset sort order
            this.PageIndex = 1;
            LoadData();
        }


        protected void Pager1_PageIndexChanging(object obj, PagerControl.PageIndexChangeEventArgs e)
        {
            this.PageIndex = e.newPageIndex;
            LoadData();
        }
        protected void Pager1_PageSizeChanging(object obj, PagerControl.PageSizeChangeEventArgs e)
        {
            this.PageIndex = 1;
            //  this.PageSize = e.newPageSize;
            LoadData();
        }
        protected void btnAddNew_Click(object sender, EventArgs e)
        {
            Response.Redirect(urlDetail);
        }
        protected void btnDelete_Click(object sender, EventArgs e)
        {
            string[] arr = hidListIdSelect.Value.Split(';');
            for (int i = 0; i < arr.Length - 1; i++)
            {
                if (arr[i].ToString() != string.Empty)
                    PhieuChuyenKhoUtil.Delete(ConvertValueObject.ParseInt(arr[i].ToString()));
            }
            LoadData();
        }
        #endregion
    }
}
